{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "1e45263e",
   "metadata": {},
   "source": [
    "# Web Data Extraction and Summarization using openAI Latest model gpt-5-mini"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "df155151",
   "metadata": {},
   "source": [
    "####         Import Libraries"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "588f8e43",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "from dotenv import load_dotenv\n",
    "from IPython.display import Markdown, display\n",
    "from openai import OpenAI \n",
    "from scrape_website import ScrapeWebsite"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b5925769",
   "metadata": {},
   "source": [
    "#### load api key"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "6cca85ec",
   "metadata": {},
   "outputs": [],
   "source": [
    "load_dotenv(override=True)\n",
    "api_key = os.getenv('OPENAI_API_KEY')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "56703f80",
   "metadata": {},
   "source": [
    "#### ScrapWebsite using  BeautifulSoup"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "3d60c909",
   "metadata": {},
   "outputs": [],
   "source": [
    "headers = {\n",
    " \"User-Agent\": \"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36\"\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a8b73c27",
   "metadata": {},
   "source": [
    "#### System Prompt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "4a0c3bda",
   "metadata": {},
   "outputs": [],
   "source": [
    "system_prompt = \"You are an analyst that analyses the content of the website \\\n",
    "    provides summary and ignore text related to navigation. Respond in markdown.\""
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9117963b",
   "metadata": {},
   "source": [
    "#### User Prompt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "ab164d55",
   "metadata": {},
   "outputs": [],
   "source": [
    "def user_prompt_for(website):\n",
    "    user_prompt = f\"You are looking at a website titled {website.title}\"\n",
    "    user_prompt += \"\\nThe contents of this website is as follows; Please provide short summary in Markdown. Please include news and \\\n",
    "        announcements\"\n",
    "    user_prompt+=website.text\n",
    "    return user_prompt"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "de7423fb",
   "metadata": {},
   "source": [
    "#### Format messages in openAI standard"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "47c82247",
   "metadata": {},
   "outputs": [],
   "source": [
    "def messages_for(website):\n",
    "    return [\n",
    "        {\"role\":\"system\", \"content\": system_prompt},\n",
    "        {\"role\":\"user\", \"content\": user_prompt_for(website)}\n",
    "    ]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6e9bb6e1",
   "metadata": {},
   "source": [
    "#### Summarise the content in website using openAI latest model gpt-5-mini"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "068d6bb2",
   "metadata": {},
   "outputs": [],
   "source": [
    "def summarise(url):\n",
    "    website = ScrapeWebsite(url, headers)\n",
    "    openai = OpenAI()\n",
    "    response = openai.chat.completions.create(model=\"gpt-5-mini\", messages=messages_for(website))\n",
    "    return response.choices[0].message.content"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7e6e9da6",
   "metadata": {},
   "source": [
    "#### Show summary as Markdown"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "cd86c2ca",
   "metadata": {},
   "outputs": [],
   "source": [
    "def display_summary(url):\n",
    "    summary = summarise(url)\n",
    "    display(Markdown(summary))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ed5e50d2",
   "metadata": {},
   "source": [
    "#### Output"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "74a056b1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/markdown": [
       "# Summary — United States Of America | Firstpost (Live/Latest)\n",
       "\n",
       "Site focus: Live updates and rundowns of US and world news with emphasis on politics, justice, economy, national security, and breaking incidents. Coverage mixes headlines, investigations, opinion and special features/web stories.\n",
       "\n",
       "## Major news (headlines)\n",
       "- Police shooting near CDC/Emory in Atlanta: a suspected shooter and a police officer were killed after reports of an active shooter near the CDC and Emory University campuses.  \n",
       "- Death of astronaut Jim Lovell (97): Apollo 13 commander and former Navy pilot died in a Chicago suburb.  \n",
       "- Stephen Miran named to Fed Board (short-term): Trump appointed economist Stephen Miran to the Federal Reserve Board through Jan 2026; noted for support of tariffs and rate cuts.  \n",
       "- Trump fires labour statistics chief: President Trump sacked the official overseeing labor data hours after a weak jobs report.  \n",
       "- House panel subpoenas Clintons over Epstein: congressional subpoenas seek documents in relation to Jeffrey Epstein amid pressure on the administration over Epstein files.  \n",
       "- Ghislaine Maxwell moved to lower-security prison in Texas amid scrutiny of Epstein files and government handling.  \n",
       "- FBI/administration tension on Epstein Files: Trump said he would “release everything” after reports the FBI redacted names from the Epstein Files.  \n",
       "- Probe launched into attorney who investigated Trump cases: US officials began a probe targeting Special Counsel Jack Smith.  \n",
       "- NTSB finds technical issues in Army helicopter crash: investigation into crash that killed 67 people identified technical problems.  \n",
       "- Trump unveils modified reciprocal tariffs: new executive order introduced modified tariffs on multiple countries; effective date possibly as late as Oct 5.  \n",
       "- Trump-EU trade deal announced: reported pact imposing a 15% tariff on most EU goods, with large energy and investment components but unresolved issues remain.  \n",
       "- Federal Reserve holds rates steady: Fed kept rates unchanged for a fifth meeting, despite political pressure from Trump.  \n",
       "- White House remodel plan: Trump pushing to build a reported $200 million ballroom at the presidential residence, funded by Trump/donors per WH.  \n",
       "- US citizenship test format under review: Trump administration considers reverting to the 2020 naturalisation test format, citing concerns the current test is too easy.  \n",
       "- American Airlines incident in Denver: passengers evacuated after a Boeing plane caught fire (tire/maintenance issue) before takeoff.  \n",
       "- John Bolton criticizes Tulsi Gabbard: former NSA lambastes Gabbard’s report on Obama as exaggerated and lacking substance.  \n",
       "- Ohio solicitor general Mathura Sridharan trolled: Indian-origin jurist faced racist online backlash after appointment; Ohio AG responded strongly.\n",
       "\n",
       "## Announcements, features & recurring elements\n",
       "- Web stories and quick-read lists: travel/animals/safety themed pieces (e.g., “10 airport codes”, “10 animals that are naturally blue”, World Tiger Day lists).  \n",
       "- Regular sections and shows highlighted in coverage: Firstpost America, Firstpost Africa, First Sports, Vantage, Fast and Factual, Between The Lines, Flashback, Live TV.  \n",
       "- Events and special coverage teased: Raisina Dialogue, Champions Trophy, Delhi Elections 2025, Budget 2025, US Elections 2024, Firstpost Defence Summit.  \n",
       "- Trending topics emphasized: Donald Trump, Narendra Modi, Elon Musk, United States, Joe Biden.  \n",
       "- Quick-links / network: cross-promotion of other Network18 properties (News18, Moneycontrol, CNBC TV18, Forbes India).\n",
       "\n",
       "## Tone and emphasis\n",
       "- Heavy focus on US politics, Trump administration actions and controversies (Epstein Files, tariffs, personnel changes), justice probes, national security incidents, and major breaking events.\n",
       "- Mix of investigative/legal reporting, immediate breaking news, and light/web-story listicles.\n",
       "\n",
       "If you want, I can produce a one-page brief of just the Trump-related items, a timeline of the Epstein/Clinton/Subpoena coverage, or extract all headlines with publication order."
      ],
      "text/plain": [
       "<IPython.core.display.Markdown object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "display_summary(\"https://www.firstpost.com/world/united-states/\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "llms",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
